package com.snail.snailvr.network;

import android.app.IntentService;
import android.content.Intent;
import android.graphics.Bitmap;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.snail.snailvr.network.upload.FormImage;
import com.snail.snailvr.utils.BitmapUtil;
import com.snail.snailvr.utils.Constant;
import com.snail.snailvr.utils.LogUtils;

import java.util.ArrayList;

/**
 * Created by sunxy
 * on 15/4/19.
 */
public class PostService extends IntentService {
    /**
     * Creates an IntentService.  Invoked by your subclass's constructor.
     */
    public PostService() {
        super("feedback");
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        ArrayList<String> picPathList = intent.getStringArrayListExtra(Constant.KEY_MULTI_PIC);
        int score = intent.getIntExtra(Constant.KEY_SCORE, 0);
        String content = intent.getStringExtra(Constant.KEY_FEEDBACK_CONTENT);
        String email = intent.getStringExtra(Constant.KEY_FEEDBACK_EMAIL);
        uploadPic(score, picPathList, content, email);
    }

    private void uploadPic(int score, ArrayList<String> picPathList, String content, String email) {
        int picCount = picPathList.size();
        ArrayList<FormImage> mBitmaps = new ArrayList<>(picCount);

        for (int i = 0; i < picCount; i++) {
            String path = picPathList.get(i);
            Bitmap bitmap = BitmapUtil.getThumblrBitmap(path);
            if (bitmap != null) {
                FormImage formImage = new FormImage(bitmap);
                mBitmaps.add(formImage);
            }
        }
        VRApi.uploadImg(score, mBitmaps, new Response.Listener() {
            @Override
            public void onResponse(Object o) {
                LogUtils.d("o=" + o.toString());
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                LogUtils.d("error=" + volleyError.getLocalizedMessage());
            }
        }, new IFSResponse() {
            @Override
            public void onSuccess(Object result) {
                
            }

            @Override
            public void onNetWorkError() {

            }

            @Override
            public void onServerError() {

            }
        }, content, email);
    }
}
